package com.gm.corejava.book1;

import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: camark@sina.cn
 * Date: 14-1-7
 * Time: 下午4:35
 */
public class CalPane extends JPanel {

    public CalPane() {

        setLayout(new BorderLayout());

        result=0;
        bStart=true;
        lastCommand="=";

        displayBtn=new JButton("0");
        displayBtn.setEnabled(false);
        add(displayBtn,BorderLayout.NORTH);

        numPanel=new JPanel();
        numPanel.setLayout(new GridLayout(4,4));

        ActionListener insertAction=new InsertAction();
        ActionListener commAction=new CommandAction();

        AddButton("7",insertAction);
        AddButton("8",insertAction);
        AddButton("9",insertAction);
        AddButton("/",commAction);

        AddButton("4",insertAction);
        AddButton("5",insertAction);
        AddButton("6",insertAction);
        AddButton("*",commAction);

        AddButton("1",insertAction);
        AddButton("2",insertAction);
        AddButton("3",insertAction);
        AddButton("-",commAction);

        AddButton("0",insertAction);
        AddButton(".",insertAction);
        AddButton("=",commAction);
        AddButton("+",commAction);

        add(numPanel,BorderLayout.CENTER);

    }

    private void AddButton(String s, ActionListener insertAction) {
        JButton button=new JButton(s);
        button.addActionListener(insertAction);
        numPanel.add(button);
    }

    private class InsertAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String eventAction=e.getActionCommand();
            if(bStart){
                displayBtn.setText("");
                bStart=false;
            }

            displayBtn.setText(displayBtn.getText()+eventAction);
        }
    }

    private class CommandAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String command=e.getActionCommand();

            if(bStart){
                if(command.equals("-")){
                    displayBtn.setText(command);
                    bStart=false;
                }
                else
                    lastCommand=command;
            }
            else
            {
                CalucatorX(Double.parseDouble(displayBtn.getText()));
                lastCommand=command;
                bStart=true;
            }
        }
    }

    private void CalucatorX(Double x){
        if(lastCommand.equals("+")) result+=x;
        else if(lastCommand.equals("-")) result-=x;
        else if(lastCommand.equals("*")) result*=x;
        else if(lastCommand.equals("/")) result/=x;
        else if(lastCommand.equals("=")) result=x;

        displayBtn.setText(""+result);

    }
    private JButton displayBtn;
    private JPanel numPanel;

    private double result;
    private String lastCommand;
    private boolean bStart;
}
